<?php
require_once 'db.php';
class UserConfig{
		public $name;
		public $para;

		function get($name=null){
			global $db;

			if($name == null)
				return false;
			$query = $db->select('userconfig',array('name'=>$name));
			if($query){
				$rs = $db->db_fetch_array($query);
				$this->name = $rs['NAME'];
				$this->para = array(
					'x'=>$rs['X'],
					'y'=>$rs['Y'],
					'PCT_FREE_MEM'=>$rs['PCT_FREE_MEM'],
					'DIC_HR'=>$rs['DIC_HR'],
					'PCT_RELOADS'=>$rs['PCT_RELOADS'],
					'BUFFER_HR'=>$rs['BUFFER_HR'],
					'REDO_WAIT_R'=>$rs['REDO_WAIT_R'],
					'IMM_ALLOC'=>$rs['IMM_ALLOC'],
					'IMM_COPY'=>$rs['IMM_COPY'],
					'WAIT_ALLOC'=>$rs['WAIT_ALLOC'],
					'WAIT_COPY'=>$rs['WAIT_COPY'],
					'PCT_SORTS'=>$rs['PCT_SORTS'],
					'ROLLBACK'=>$rs['ROLLBACK']
					);
			}
			else
				return false;
		}
		
		function UserConfig($name=null){
			$this->get($name);
		}

		/**
		 * must get the user first to remove the user
		 */
		function remove(){
			global $db;
			return $db->remove('userconfig',array('name'=>$this->name));
		}
		function add(){
			global $db;
			$update = array(
				'name' => $this->name,
				'x' => $this->para['x'],
				'y' => $this->para['y'],
				'PCT_FREE_MEM'=>$this->para['PCT_FREE_MEM'],
				'DIC_HR'=>$this->para['DIC_HR'],
				'PCT_RELOADS'=>$this->para['PCT_RELOADS'],
				'BUFFER_HR'=>$this->para['BUFFER_HR'],
				'REDO_WAIT_R'=>$this->para['REDO_WAIT_R'],
				'IMM_ALLOC'=>$this->para['IMM_ALLOC'],
				'IMM_COPY'=>$this->para['IMM_COPY'],
				'WAIT_ALLOC'=>$this->para['WAIT_ALLOC'],
				'WAIT_COPY'=>$this->para['WAIT_COPY'],
				'PCT_SORTS'=>$this->para['PCT_SORTS'],
				'ROLLBACK'=>$this->para['ROLLBACK']
			);
			return $db->insert('userconfig',$update);
		}
		function update(){
			global $db;
			$update = array(
				'name' => $this->name,
				'x' => $this->para['x'],
				'y' => $this->para['y'],
				'PCT_FREE_MEM'=>$this->para['PCT_FREE_MEM'],
				'DIC_HR'=>$this->para['DIC_HR'],
				'PCT_RELOADS'=>$this->para['PCT_RELOADS'],
				'BUFFER_HR'=>$this->para['BUFFER_HR'],
				'REDO_WAIT_R'=>$this->para['REDO_WAIT_R'],
				'IMM_ALLOC'=>$this->para['IMM_ALLOC'],
				'IMM_COPY'=>$this->para['IMM_COPY'],
				'WAIT_ALLOC'=>$this->para['WAIT_ALLOC'],
				'WAIT_COPY'=>$this->para['WAIT_COPY'],
				'PCT_SORTS'=>$this->para['PCT_SORTS'],
				'ROLLBACK'=>$this->para['ROLLBACK']
			);
			return $db->update('userconfig',$update,array('name'=>$this->name));
		}
		
		function getFirst($field_name) {
			$num = (int)$this->para[$field_name];
			if($num < 10000)
				return 0;
			else {
				$str = $num + "";
				return (int)substr($str, 0, strlen($str)-4) / 100.0;
			}
		}
		
		function getSecond($field_name) {
			$num = (int)$this->para[$field_name];
			return $num % 10000 / 100.0;
		}

	}
?>